//获取应用实例
const app = getApp()
const util = require('../../../utils/util.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loadding : 0,
    accounts: [],
    deviceTypes:[],
    accountIndex: -1,
    typeIndex: -1,
    date :"",
    checkpointType:[],
    checkpointTypeIndex:-1,
    pointTypeName:"点击此处选择",
    selectVisble:false
  },
  onLoad: function(options) {
    console.log("nfc绑定界面",options.aid)
    this.setData({
      aid:options.aid
    })
    let checkpointType=[]
    checkpointType.push({
      type: 2,
      item:"巡检",
      checked: false
    })
    checkpointType.push({
      type: 3,
      item:"值周",
      checked: false
    })
    this.setData({
      checkpointType:checkpointType
    })
    wx.request({
      url: app.inspectUrl + '/WeChat/weChatSystem/getNFCategoryAndType',
      method: 'POST',
      dataType: 'json',
      success: res => {
        let accounts = []
        for (let i = 0; i < res.data.length; i++) {
          accounts.push({
            item: res.data[i].category,
            checked: false
          })
        }
        this.setData({
          accounts: accounts,
          result: res.data
        })
      }
    })
    //查看是否有该设备，没有输入绑定，有则将显示设备字段
    wx.request({
      url: app.inspectUrl + '/WeChat/weChatSystem/getNFCDeviceById',
      method: 'POST',
      dataType: 'json',
      data: {
        schoolId : parseInt(app.globalData.departmentId) ,
        macAddress : options.aid
      },
      success: res => {
        if(res.data.deviceName != null){
          this.setData({
            date : res.data.validTime,
            deviceCategory : res.data.category,
            deviceType : res.data.deviceName,
          })
        }else{
          this.setData({
            deviceCategory :'点击此处选择',
            deviceType :'点击此处选择',
          })
        }
      }
    })
  },
  showModal(e) {
    let accounts = this.data.accounts.map((item, index) => {
      return {
        item: item.item,
        checked: index == this.data.accountIndex ? true : false
      }
    })
    this.setData({
      modalName: "RadioModal",
      accounts: accounts,
      modalComfirm: this.data.selectedIndex != null ? true : false,
      // forwardId: e.currentTarget.dataset.id
    })
  },
  showModal2(e) {
    if(this.data.accountIndex != -1){
      let deviceTypes = this.data.result[this.data.accountIndex].deviceType.split(",").map((item, index) => {
        return {
          item: item,
          checked: index == this.data.typeIndex ? true : false
        }
      })
      this.setData({
        modalName2: "RadioModal",
        deviceTypes: deviceTypes,
        modalComfirm2: this.data.selectedIndex2 != null ? true : false,
        // forwardId: e.currentTarget.dataset.id
      })
    }else{
      wx.showToast({
        title: '设备类别未输入',
        image: "../../../images/error.png"
      })
    }
  },
  showModal3(e) {
    let checkpointType = this.data.checkpointType.map((item, index) => {
      return {
        type:item.type,
        item: item.item,
        checked: index == this.data. checkpointTypeIndex ? true : false
      }
    })
    this.setData({
      modalName3: "RadioModal",
      checkpointType: checkpointType,
      modalComfirm3: this.data.selectedIndex3 != null ? true : false,
      // forwardId: e.currentTarget.dataset.id
    })
  },

  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  hideModal2(e) {
    this.setData({
      modalName2: null
    })
  },
  hideModal3(e) {
    this.setData({
      modalName3: null
    })
  },

  radioSelect(e) {
    console.log("radioIndex",e.currentTarget.dataset.index)
    let radioIndex = e.currentTarget.dataset.index,
      accounts = this.data.accounts.map((item, index) => {
        return {
          item: item.item,
          checked: index == radioIndex ? true : false
        }
      }),
      status
    this.setData({
      accounts: accounts,
      selectedIndex: radioIndex
    })
    if (this.data.selectedIndex != null) status = true
    else status = false
    this.setData({
      modalComfirm: status
    })
  },
  radioSelect2(e) {
    console.log("radioIndex2",e.currentTarget.dataset.index)
    let radioIndex2 = e.currentTarget.dataset.index,
    deviceTypes = this.data.deviceTypes.map((item, index) => {
        return {
          item: item.item,
          checked: index == radioIndex2 ? true : false
        }
      }),
      status
    this.setData({
      deviceTypes: deviceTypes,
      selectedIndex2: radioIndex2
    })
    if (this.data.selectedIndex2 != null) status = true
    else status = false
    this.setData({
      modalComfirm2: status
    })
  },
  radioSelect3(e) {
    console.log("radioIndex3",e.currentTarget.dataset.index)
    let radioIndex3 = e.currentTarget.dataset.index,
    checkpointType = this.data.checkpointType.map((item, index) => {
        return {
          type:item.type,
          item: item.item,
          checked: index == radioIndex3 ? true : false
        }
      }),
      status
    this.setData({
      checkpointType: checkpointType,
      selectedIndex3: radioIndex3
    })
    if (this.data.selectedIndex3 != null) status = true
    else status = false
    this.setData({
      modalComfirm3: status
    })
  },

  confirmSelect(e) {
    this.setData({
      accountIndex: this.data.selectedIndex,
      modalName: null
    })
  },
  confirmSelect2(e) {
    if(this.data.deviceTypes[this.data.selectedIndex2].item != '灭火器'){
      this.setData({
        date:""
      })
    }
    this.setData({
      deviceType:this.data.deviceTypes[this.data.selectedIndex2].item,
      typeIndex: this.data.selectedIndex2,
      modalName2: null
    })
  },
  confirmSelect3(e) {
    this.setData({
      checkpointTypeIndex: this.data.selectedIndex3,
      modalName3: null
    })
    console.log("index"+this.data.checkpointType[this.data.checkpointTypeIndex].item);
    if(this.data.checkpointType[this.data.checkpointTypeIndex].type==2){
      this.setData({

        selectVisble:true,
      })
    }
    else if(this.data.checkpointType[this.data.checkpointTypeIndex].type==3){
      this.setData({

        selectVisble:false,
      })
    }
  },
  getAddress: function(e) {
    this.setData({
      address: e.detail.value
    })
  },
  formSubmit: util.throttle(function(e) {
     console.log("this.data.date",this.data.date)
     let waringInfo = ""
     if (this.data.checkpointTypeIndex == -1 && this.data.pointTypeName == "点击此处选择") {
      waringInfo = "请选择打卡类别"
     } 
     else if (this.data.checkpointType[this.data.checkpointTypeIndex].type==2 && this.data.accountIndex == -1 && this.data.deviceCategory == "点击此处选择") {
      waringInfo = "请选择设备类型"
     } else if (this.data.checkpointType[this.data.checkpointTypeIndex].type==2 && this.data.typeIndex == -1 && this.data.deviceType == "点击此处选择") {
      waringInfo = "请选择设备名称"
     } else if (e.detail.value.address == "") {
      waringInfo = "请选择设备地点"
     } else if(this.data.deviceType == '灭火器' && this.data.date == ""){
      waringInfo = "选择设备有效期"
     }else{
      wx.showLoading({
        title: "正在绑定"
      })
      let category1  ;
      let deviceName1 ;
      if(this.data.checkpointType[this.data.checkpointTypeIndex].type==2){
      if(this.data.accountIndex ==-1 && this.data.deviceType != "点击此处选择"){
        category1 = this.data.deviceCategory
        deviceName1 = this.data.deviceType
      }else{
        category1 = this.data.accounts[this.data.accountIndex].item
        if(this.data.typeIndex == -1){
          deviceName1 = this.data.deviceType
        }else{
          deviceName1 = this.data.deviceTypes[this.data.typeIndex].item
        }
      }
      }
      wx.request({
        url: app.inspectUrl + '/WeChat/weChatSystem/NFCBindingInfo',
        method: 'POST',
        data: {
          schoolId: app.globalData.departmentId,
          address: this.data.address,
          aid: this.data.aid, 
          category: category1,
          deviceName: deviceName1,
          validTime:this.data.date,
          pointType:this.data.checkpointType[this.data.checkpointTypeIndex].type
         
        },
        dataType: 'json',
        success: res => {
          console.log("res",res)
          if(res.data == 'success'){
            wx.showToast({
              title: "绑定成功"
            })
            // wx.navigateTo({
            //   url: '../../../subpages/pages/nfcResponse/nfcResponse'
            // })
            setTimeout(function() {
              let pages = getCurrentPages(); //当前页面
              let prevPage = pages[pages.length - 2]; //上一页面
              prevPage.setData({ //直接给上移页面赋值
                submitRefresh: 1
              });
              // 提示框隐藏后 跳转
              wx.navigateBack({
                delta: 1
              })
            }, 1000)
          }
        },
        fail: () => {
          wx.showToast({
            title: '网络异常！',
            mask: true,
            image: "/images/error.png"
          })
        }
      })
    }
    if (waringInfo != "") {
      wx.showToast({
        title: waringInfo,
        image: "/images/warning.png"
      })
    }
    
  }),
  closeNFC:function(){
    let NFCAdapter = wx.getNFCAdapter() ;
    NFCAdapter.offDiscovered(callback => {});
    NFCAdapter.stopDiscovery();
    console.log("关闭监听2")
  },
   //日期选择器
   bindDateChange:function(e){
    console.log(e.detail.value)
    this.setData({
      date : e.detail.value
    })
  },
})

